<template>
  <div class="login-wrap">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      form: {
        name: "",
        password: "",
      },
    };
  },
  methods: {
    login() {
      // 发送请求
      axios
        .post("/v1/login", {
          name: this.form.name,
          password: this.form.password,
        })
        .then((res) => {
          if (res.data.code == 1) {
            // 登录成功，跳转路由到首页

            localStorage.setItem("name", this.form.name);

            this.$router.push({
              name: "home",
            });
          } else {
            // 登录失败，提示
            this.$message("登录失败");
          }
        });
    },
  },
};
</script>

<style>
.login-wrap {
  width: 300px;
  margin: 200px auto;
}
</style>